<?php
/*首页数据接口*/
namespace Home\Controller;
use Think\Controller;
class UploadController extends Controller {
    public function studentExcel(){   
        import("Org.Util.PHPExcel");                                 // 导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        $PHPExcel = new \PHPExcel();                                 // 创建PHPExcel对象，注意，不能少了\
        $exts="xlsx";
        if ($exts == 'xls') {                                        // 如果excel文件后缀名为.xls，导入这个类
            import("Org.Util.PHPExcel.Reader.Excel5");
            $PHPReader = new \PHPExcel_Reader_Excel5();
        } else if ($exts == 'xlsx') {
            import("Org.Util.PHPExcel.Reader.Excel2007");
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
        $PHPExcel = $PHPReader->load("./Public/jdc_car_details.xlsx");                     // 载入文件
        $currentSheet = $PHPExcel->getSheet(0);                      // 获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
        
        $allRow = $currentSheet->getHighestRow();                    // 获取总行数
        $cellNameArr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S');
        $allColumn = count($cellNameArr);              // 获取总列数
        $columnArr=[
            'A'=>"carnum",
            'B'=>"base_info",
            'C'=>"config_spotlight",
            'D'=>"models_spotlight",
            'E'=>"body_config",
            'F'=>"engine_config",
            'G'=>"gearbox_config",
            'H'=>"chassis_steering",
            'I'=>"wheel_brake",
            'J'=>"safety_equip",
            'K'=>"control_config",
            'L'=>"outside_config",
            'M'=>"inside_config",
            'N'=>"chair_config",
            'O'=>"multimedia_config",
            'P'=>"light_config",
            'Q'=>"mirrors_config",
            'R'=>"aircondition_config",
            'S'=>"hightech_config"];
        
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow ++) {// 循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
            for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn ++) {// 从哪列开始，A表示第一列
                $address = $cellNameArr[$currentColumn] . $currentRow;             // 数据坐标
                $carnum=$currentSheet->getCell("A".$currentRow)->getValue();
                $value=$currentSheet->getCell($address)->getValue();
                $content_arr=[];
                if($currentColumn>0){
                    $val_arr=explode('|',$value);
                    foreach($val_arr as $v){
                        $val_arr1=explode(':',$v);
                        $content_arr[]=$val_arr1;
                    }
                    $ExlData[$carnum][$columnArr[$cellNameArr[$currentColumn]]] = json_encode($content_arr);// 读取到的数据，保存到数组$arr中
                }
                    
            }
        }
        $car_details = M('car_details');  
                                             // 生成数据库对象
        foreach($ExlData as $key=>$val){
            $result=$car_details->where("carnum='".$key."'")->save($val);
            print($car_details->getLastSql());  
            var_dump($result);
        }
    }
}
